// 下载 具体的小说章节
let axios = require("axios")
let cheerio = require("cheerio")

require("./connect.js")

let {
    Book
} = require("./mod/bookcover.js")
let {
    Chapter
} = require("./mod/chapter.js")


// 获取具体的章节内容
async function getchapters() {
    // 从 book表中取出arr
    let booklists = await Book.find()

    for (let i = 40; i < booklists.length; i++) {
        let book_title = booklists[i].book_title
        for (let j = 0; j < booklists[i].url_arr.length; j++) {
            let url = booklists[i].url_arr[j]["chapter_url"]
            let chapter_title = booklists[i].url_arr[j]["chapter_title"]
            try{
                let res = await axios.get(url,{
                    headers: {'Cookie': 'bcolor=; font=; size=; fontcolor=; width=','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36 Edg/87.0.664.41'},})
                let $ = cheerio.load(res.data)
                let chapter_content = $("#content").html().trim()
                chapter_title = book_title + "___" + chapter_title
    
                // 保存章节信息
                try {
                    new Chapter({
                        chapter_content,
                        chapter_title,
                        book_title
                    }).save()
                } catch {
                    console.log('章节保存失败')
                }
            }catch(err){
                console.log(err)
            }
        }
    }
    
}


getchapters()